package code20_jdbc.改写学生管理.view;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Scanner;

import code20_jdbc.改写学生管理.dao.StudentDao;
import code20_jdbc.改写学生管理.entity.Clazz;
import code20_jdbc.改写学生管理.entity.Student;

public class StudentView {
	Scanner input = new Scanner(System.in);
	StudentDao studentDao = new StudentDao();
	
	public void showMenu(){
		System.out.println("-----------------------------");
		System.out.println("1. 查询所有学生");
		System.out.println("2. 根据学号查询学生");
		System.out.println("3. 根据姓名和年龄范围查询学生");
		System.out.println("4. 添加学生");
		System.out.println("5. 修改学生");
		System.out.println("6. 根据学号删除学生");
		System.out.print("请选择：");
		int choice = input.nextInt();
		switch(choice){
			case 1:
				findAll();
				break;
			case 2:
				findById();
				break;
			case 3:
				findByCondition();
				break;
			case 4:
				add();
				break;
			case 5:
				modify();
				break;
			case 6:
				removeById();
				break;
		}
		
	}

	private void findAll() {
		List<Student> list = studentDao.selectAll();
		System.out.println(list);
	}

	private void findById() {
	}

	private void findByCondition() {
	}

	// 添加学生
	private void add() {
		/*
		 * 视图操作
		 */
		Student stu = new Student();
		System.out.print("请输入学生姓名：");
		stu.setName(input.next());
		System.out.print("请输入年龄：");
		stu.setAge(input.nextInt());
		System.out.print("请输入身高：");
		stu.setHeight(input.nextDouble());
		System.out.print("请输入出生日期：");
		try {
			stu.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(input.next()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		System.out.print("请输入班级号：");
		stu.setClazz(new Clazz(input.nextInt()));
		
		/*
		 * 数据库操作
		 */
		studentDao.insert(stu);
		System.out.println("添加成功");
	}

	private void modify() {
		
	}

	private void removeById() {
		System.out.print("请输入要删除的学生学号：");
		int id = input.nextInt();
		
		studentDao.deleteById(id);
		System.out.println("删除成功");
	}
}
